<template>
  <div class="responsive-table-container">
    <!-- 桌面端表格 -->
    <div v-if="!isMobile" class="table-responsive-enhanced custom-scrollbar">
      <table class="table table-hover table-fixed-columns">
        <thead class="table-light">
          <tr>
            <th v-for="column in columns" :key="column.key" :style="getColumnStyle(column)">
              {{ column.title }}
            </th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in data" :key="getRowKey(item, index)">
            <td v-for="column in columns" :key="column.key" :style="getColumnStyle(column)">
              <slot :name="column.key" :item="item" :index="index">
                {{ getColumnValue(item, column) }}
              </slot>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- 移动端卡片布局 -->
    <div v-else class="mobile-card-list">
      <div v-for="(item, index) in data" :key="getRowKey(item, index)" class="mobile-card mb-3">
        <div class="card">
          <div class="card-body">
            <div v-for="column in visibleMobileColumns" :key="column.key" class="mobile-field mb-2">
              <div class="mobile-field-label">{{ column.title }}</div>
              <div class="mobile-field-value">
                <slot :name="column.key" :item="item" :index="index">
                  {{ getColumnValue(item, column) }}
                </slot>
              </div>
            </div>
            <!-- 操作按钮 -->
            <div v-if="hasActions" class="mobile-actions mt-3">
              <slot name="actions" :item="item" :index="index"></slot>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div v-if="data.length === 0" class="empty-state text-center py-5">
      <slot name="empty">
        <i class="bi bi-inbox display-1 text-muted"></i>
        <p class="mt-3 text-muted">暂无数据</p>
      </slot>
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted, onUnmounted, ref } from 'vue'

const props = defineProps({
  columns: {
    type: Array,
    required: true,
    validator: (columns) => {
      return columns.every(col => col.key && col.title)
    }
  },
  data: {
    type: Array,
    default: () => []
  },
  rowKey: {
    type: [String, Function],
    default: 'id'
  },
  mobileBreakpoint: {
    type: Number,
    default: 768
  },
  mobileVisibleColumns: {
    type: Array,
    default: () => []
  }
})

const screenWidth = ref(window.innerWidth)

const isMobile = computed(() => {
  return screenWidth.value <= props.mobileBreakpoint
})

const visibleMobileColumns = computed(() => {
  if (props.mobileVisibleColumns.length > 0) {
    return props.columns.filter(col => props.mobileVisibleColumns.includes(col.key))
  }
  // 默认显示前4个列（排除操作列）
  return props.columns.filter(col => col.key !== 'actions').slice(0, 4)
})

const hasActions = computed(() => {
  return props.columns.some(col => col.key === 'actions')
})

const getRowKey = (item, index) => {
  if (typeof props.rowKey === 'function') {
    return props.rowKey(item, index)
  }
  return item[props.rowKey] || index
}

const getColumnValue = (item, column) => {
  if (column.render) {
    return column.render(item)
  }
  return item[column.key] || '-'
}

const getColumnStyle = (column) => {
  const style = {}
  if (column.width) {
    style.width = column.width
    style.minWidth = column.width
  }
  if (column.align) {
    style.textAlign = column.align
  }
  return style
}

const handleResize = () => {
  screenWidth.value = window.innerWidth
}

onMounted(() => {
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.responsive-table-container {
  width: 100%;
}

.table-responsive-enhanced {
  position: relative;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 0.375rem;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.05);
}

.table-fixed-columns {
  min-width: 100%;
  table-layout: auto;
}

.table-fixed-columns th,
.table-fixed-columns td {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0.75rem 0.5rem;
}

.table-fixed-columns th:first-child,
.table-fixed-columns td:first-child {
  position: sticky;
  left: 0;
  background: white;
  z-index: 10;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
  min-width: 100px;
}

.table-fixed-columns th:last-child,
.table-fixed-columns td:last-child {
  position: sticky;
  right: 0;
  background: white;
  z-index: 10;
  box-shadow: -2px 0 5px rgba(0, 0, 0, 0.1);
  min-width: 120px;
}

.mobile-card-list {
  padding: 0;
}

.mobile-card {
  border-radius: 0.5rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.mobile-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.mobile-field {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 0.25rem 0;
  border-bottom: 1px solid #f0f0f0;
}

.mobile-field:last-child {
  border-bottom: none;
}

.mobile-field-label {
  font-weight: 600;
  color: #6c757d;
  font-size: 0.85rem;
  min-width: 80px;
  flex-shrink: 0;
}

.mobile-field-value {
  text-align: right;
  font-size: 0.9rem;
  color: #495057;
  word-break: break-word;
}

.mobile-actions {
  border-top: 1px solid #e9ecef;
  padding-top: 0.75rem;
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.empty-state {
  padding: 3rem 1rem;
}

/* 响应式优化 */
@media (max-width: 576px) {
  .mobile-field {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.25rem;
  }
  
  .mobile-field-label {
    min-width: auto;
  }
  
  .mobile-field-value {
    text-align: left;
    width: 100%;
  }
  
  .mobile-actions {
    justify-content: center;
  }
}
</style>